<?php
class Money_Api_Core extends Core_Api_Abstract
{
  public function getTransactionPaginator($params = array())
  {
    $paginator = Zend_Paginator::factory($this->getTransactionSelect($params));
    if( !empty($params['page']) )
    {
      $paginator->setCurrentPageNumber($params['page']);
    }
    if( !empty($params['limit']) )
    {
      $paginator->setItemCountPerPage($params['limit']);
    }
    return $paginator;
  }

  public function getTransactionSelect($params = array())
  {
    $table = Engine_Api::_()->getDbtable('transactions', 'money');
    $rName = $table->info('name');
    $userTable = Engine_Api::_()->getItemTable('user');
    $userTableName = $userTable->info('name');
    
    $select = $table->select()
                    ->from($rName)
                    ->joinLeft($userTableName, "`{$userTableName}`.`user_id` = `{$rName}`.`user_id`", null)
                    ->order('transaction_id DESC');
    
    if(!empty($params['type'])){
        $select->where('type = ?', $params['type']);
    }
    if(!empty($params['text'])){
        $select->where("`{$userTableName}`.`username` LIKE ?", "%{$params['text']}%");
    }
    
    if(!empty($params['gateway'])){
        $select->where('gateway_id =?', $params['gateway']);
    }
    
    if(!empty($params['user_id'])){
        $select->where("`{$rName}`.`user_id` = ?", $params['user_id']);
    }
    
    return $select;
  }
}
